<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//
require_once( 'Gecko/Controller/Exception.php' );

/**
 * Router Controller
 *
 * @package Gecko;
 * @author Christopher Valderrama <valderrama.christopher@gmail.com>
 * @copyright Copyright (c) 2008
 * @version $Id$v2.0$ 2008
 * @access public
 **/
class Gecko_Controller {
	/**
	 * The name of the View object to use
	 **/
	private $_view = 'Gecko_View';

	/**
	 * Name of the template to use
	 **/
	private $_useTemplate = '';

	/**
	 * Name of the layout to use
	 **/
	private $_useLayout = '';

	/**
	 * Creates a new controller, put your custom
	 * logic here to setup your controller
	 *
	 **/
	public function __construct(){}

	/**
	 * Returns the Action to perform
	 *
	 * @access public
	 * @return string Current Action
	 **/
	public function getAction() {
		$base = Zend_Registry::get( 'base' );
		return $base->getAction();
	}

	/**
	 * Returns the Controller name
	 *
	 * @access public
	 * @return string Controller name
	 **/
	public function getControllerName() {
		return str_replace( "Controller", "", get_class( $this ) );
	}

	/**
	 * This function returns a URI to a action in the same controller
	 *
	 * @param string $action The action name
	 * @param int $type The action URI Type (0 = dir, 1 = html)
	 * @access protected
	 * @return string
	 **/
	public function linkToAction( $action, $type = 0 ) {
		$cfg = Zend_Registry::get("config");
		$controller = get_class( $this );
		$controller = str_replace( "Controller", "", $controller );
		$chkAction = $action . "Action";

		if( !is_callable( array( $this, $chkAction ) ) ) {
			throw new Gecko_Controller_Exception( "Requested action can't be called from this controller" );
		}

		$end = $cfg->URITermination;
		$terminator = "/";
		if( $end ) {
			switch( $end ) {
			default:
			case 'dir':
				$terminator = "/";
				break;
			case 'html':
				$terminator = ".html";
				break;
			}
		}

		$destination = $cfg->pageRootURL . "/" . $controller . "/" . $action . $terminator;

		return $destination;
	}

	/**
	 * Stops the Controller from rendering a View
	 *
	 * @access public
	 * @return void
	 **/
	public function noRender() {
		exit();
	}

	/**
	 * Returns the Configuration object
	 *
	 * @access public
	 * @return object Zend_Config
	 **/
	public function getConfig() {
		return Zend_Registry::get('config');
	}

	/**
	 * Redirect to a new action/destination
	 * inside the Router
	 *
	 * @param string $place
	 * @param int $type
	 * @access protected
	 * @return void
	 **/
	protected function redirect( $place, $type = 0 ) {
		$cfg = Zend_Registry::get("config");

		$end = $cfg->URITermination;
		$terminator = "/";
		if( $end ) {
			switch( $end ) {
			default:
			case 'dir':
				$terminator = "/";
				break;
			case 'html':
				$terminator = ".html";
				break;
			}
		}

		if( is_array( $place ) ) {
			$location = $cfg->pageRootURL . "/";
			if( isset( $place[0] ) && is_object( $place[0] ) ) {
				$controller .= get_class( $place[0] ) . "/";
				$controller = str_replace( "Controller", "", $controller );
				$location .= $controller;
			} elseif( !empty( $place[0] ) ) {
				$location .= $place[0] . "/";
			}
			if( !empty( $place[1] ) ) {
				$location .= $place[1] . $terminator;
			}
		} else {
			$location = $cfg->pageRootURL . $place;
		}

		$this->_redirect( $location );
	}

	/**
	 * Performs the actual redirection
	 *
	 * @param string the URL to redirect
	 * @access public
	 * @return void
	 **/
	public function _redirect( $url ) {
		if( headers_sent() ) {
			throw new Gecko_Controller_Exception( "Headers Already Sent! Cannot redirect, please check your code" );
		}

		header( "Location: " . $url );
		exit();
	}

	/**
	 * Called when no action in controller has been found
	 *
	 * @access public
	 * @return void
	 **/
	public function noRoute() {
		throw new Gecko_Controller_Exception( "Action couldn't be routed in controller" );
	}

	/**
	 * Saves a Session Message for later displaying
	 *
	 * @param string $msg
	 * @access public
	 * @return void
	 **/
	public function saveMsg( $msg ) {
		Gecko_Utils::saveMsg( $msg );
	}

	/**
	 * Get Variables from controller
	 *
	 * @access public
	 * @return array
	 **/
	public function getVars() {
		$vars = get_object_vars( $this );

		return $vars;
	}

	/**
	 * Returns the View name registered
	 *
	 * @access public
	 * @return string
	 **/
	public function getView() {
		return $this->_view;
	}

	/**
	 * Finds if a template is set in the controller
	 *
	 * @access public
	 * @return boolean
	 **/
	public function isTemplateSet() {
		return !empty( $this->_useTemplate );
	}

	/**
	 * Get's the Registered Template
	 *
	 * @access public
	 * @return string
	 **/
	public function getTemplate() {
		return $this->_useTemplate;
	}

	/**
	 * Sets the View Active Template
	 *
	 * @param string $template
	 * @access protected
	 * @return void
	 **/
	protected function setViewTemplate( $template ) {
		$this->_useTemplate = $template;
	}

	/**
	 * finds if a layout has been set
	 *
	 * @access public
	 * @return boolean
	 **/
	public function isLayoutSet() {
		return !empty( $this->_useLayout );
	}

	/**
	 * Returns the layout set
	 *
	 * @access public
	 * @return string
	 **/
	public function getLayout() {
		return $this->_useLayout;
	}

	/**
	 * Sets the active Layout
	 *
	 * @param string $layout
	 * @access public
	 * @return void
	 **/
	protected function setLayout( $layout ) {
		$this->_useLayout = $layout;
	}

	/**
	 * Sets the active View
	 *
	 * @param string $view
	 * @return void
	 **/
	protected function setView($view) {
		$this->_view = $view;
	}
}